package com.activiti01;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ActivitiDemo {

    /**
     * 测试流程部署
     */
    @Test
    public void testDeployment() {
        //创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //使用RepositoryService来进行流程的部署，就是把bpmn文件和png文件部署到数据库里
        //还需要给流程定义一个名字
        Deployment deploy = repositoryService.createDeployment()
                .name("测试会签")
                .addClasspathResource("BpmnFile/cshq.bpmn")
                .deploy();

        //输出部署信息
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Test
    public void contextLoads() {
    }
    @Test
    public void testStartProcess() {

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String,Object> variables = new HashMap<>();
        variables.put("oneNodeUserList", Arrays.asList("zhangsan","lisi"));
        variables.put("twoNodeUserList", Arrays.asList("zhangsan","lisi"));
//        variables.put("twoNodeUserList", Arrays.asList("wangwu","zhaoliu"));
        ProcessInstance start = runtimeService.startProcessInstanceByKey("cehq", variables);
        //输出流程信息
        System.out.println("流程ID:" + start.getId());
        System.out.println("流程名称:" + start.getName());
        System.out.println("流程定义ID:" + start.getProcessDefinitionId());
        System.out.println("流程定义Name:" + start.getProcessDefinitionKey());
        System.out.println("流程定义版本:" + start.getProcessDefinitionVersion());
    }

    /**
     * 查询当前个人待执行的任务
     */
    @Test
    public void testFindPersonalTaskList() {
        testFindPersonalTaskList("zhangsan");
        testFindPersonalTaskList("lisi");
        testFindPersonalTaskList("wangwu");
        testFindPersonalTaskList("zhaoliu");
    }
    private void  testFindPersonalTaskList(String assignee){
        //任务负责人
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //创建TaskService
        TaskService taskService = processEngine.getTaskService();
        //根据流程key 和 任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("cehq").taskAssignee(assignee).list();
        System.out.println("----------------------");
        System.out.println("用户：" + assignee+"任务数量:"+list.size());
        System.out.println("----------------------");

    }
    // 完成任务
    @Test
    public void completTask(){

    }
    public void completTask(String assignee){
//        获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        获取taskService
        TaskService taskService = processEngine.getTaskService();

//        根据流程key 和 任务的负责人 查询任务
//        返回一个任务对象
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("cehq") //流程Key
                .taskAssignee(assignee)  //要查询的负责人
                .singleResult();
//        完成任务,参数：任务id
        taskService.complete(task.getId());
        System.out.println("完成任务：" + task.getId()+"  任务名称："+task.getName());
    }


    @Test
    public void  queryBpmnFile() throws IOException {
        testStartProcess();
        completTask("zhangsan");
        completTask("lisi");
        completTask("zhangsan");
        completTask("lisi");
//        testFindPersonalTaskList();

//        completTask("zhaoliu");
//        completTask("wangwu");

    }
}
